package com.itheima.mythread.Demo11;

public class Test {
    public static void main(String[] args) {
        Desk desk = new Desk();

        int count = 7;
         new Thread(()->{
             for (int i = 1; i <=count; i++) {
                 synchronized (desk){
                     if (desk.isFlag()){
                         try {
                             desk.wait();
                             i--;
                         } catch (InterruptedException e) {
                             e.printStackTrace();
                         }
                     }else{
                         desk.put(i);
                         desk.setFlag(true);
                         desk.notify();
                     }
                 }
             }
         }).start();

        Thread consumer = new Thread(()->{
             while (true){
                 synchronized (desk){
                     if (desk.isFlag()){
                         desk.take();
                         desk.setFlag(false);
                         desk.notify();
                     }else{
                         try {
                             desk.wait();
                         } catch (InterruptedException e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
         });

        consumer.setDaemon(true);

        consumer.start();
    }
}
